using System;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
	/**
	 * Classname : NguoiTimNguon
	 * Version information :1.0
	 * Date  :26-09-2006
	 * Copyright notice : 
	 */
	
	public class DANguoiTimNguon : DbObject
	{
		protected SqlConnection connec;

        public DANguoiTimNguon(SqlConnection Connection)
		{
            connec = Connection;
		}	
		// return all the NguoiTimNguons select Language
		// Danh cho nguoi quan tri
		public DataSet loadNguoiTimNguon()
		{
			// create the parameter
            DataSet dsNguoiTimNguon = RunProcedure("sp_NguoiTimNguon_loadNguoiTimNguon", "TB_NguoiTimNguon", connec);
            for (int i = 0; i < dsNguoiTimNguon.Tables[0].Rows.Count; i++)
            {
                dsNguoiTimNguon.Tables[0].Rows[i]["Seq"] = (i + 1).ToString();
            }
            return dsNguoiTimNguon;
		}
        public DataSet loadNguoiTimNguonbyNguon(int NguonID)
        {
            // create the parameter
            SqlParameter[] parameters = { new SqlParameter("@NguonID", SqlDbType.Int, 4) };
            parameters[0].Value = NguonID;

            DataSet dsNguoiTimNguon = RunProcedure("sp_NguoiTimNguon_loadNguon", parameters, "TB_NguoiTimNguon", connec);
            for (int i = 0; i < dsNguoiTimNguon.Tables[0].Rows.Count; i++)
            {
                dsNguoiTimNguon.Tables[0].Rows[i]["Seq"] = (i + 1).ToString();
            }
            return dsNguoiTimNguon;
        }
		
		// return only the record with the specified ID!!!!!!!!!!!
		public DataRow loadNguoiTimNguonID(int NguoiTimNguonId)
		{
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4) };
			parameters[0].Value = NguoiTimNguonId;

            using (DataSet NguoiTimNguons = RunProcedure("sp_NguoiTimNguon_loadNguoiTimNguonID", parameters, "TB_NguoiTimNguon", connec))
			{
				return NguoiTimNguons.Tables[0].Rows[0];
			}
		}	


		// update the properties of the specified NguoiTimNguons
		// Sua noi dung ban ghi hien hanh
  
		public int updateNguoiTimNguon(int NguoiTimNguonId, string strHoTen,string strDiaChi,string strDienThoai,string strEmail,
                                           string strDiDong,string strFax,string strGhiChu,bool boolGioiTinh,int NguonID)
		{
			int numAffected;
			// create the parameters
            SqlParameter[] parameters = { 	new SqlParameter("@ID", SqlDbType.Int, 4),
											new SqlParameter("@HoTen", SqlDbType.NVarChar, 256),
                                            new SqlParameter("@DiaChi", SqlDbType.NVarChar, 256),
                                            new SqlParameter("@DienThoai", SqlDbType.VarChar, 20),                                       
                                            new SqlParameter("@Email", SqlDbType.VarChar, 64),
                                            new SqlParameter("@DiDong", SqlDbType.VarChar, 20),
                                            new SqlParameter("@Fax", SqlDbType.VarChar, 20),
                                            new SqlParameter("@GhiChu", SqlDbType.NVarChar, 1024),
                                             new SqlParameter("@GioiTinh", SqlDbType.Bit),
                                             new SqlParameter("@NguonID", SqlDbType.Int)
										};
			// set the values
			parameters[0].Value = NguoiTimNguonId;
            parameters[1].Value = strHoTen.Trim();
            parameters[2].Value = strDiaChi.Trim();
            parameters[3].Value = strDienThoai.Trim();
            parameters[4].Value = strEmail.Trim();
            parameters[5].Value = strDiDong.Trim();
            parameters[6].Value = strFax.Trim();
            parameters[7].Value = strGhiChu.Trim();
            parameters[8].Value = boolGioiTinh;
            parameters[9].Value = NguonID;    

			// run the procedure
            RunProcedure("sp_NguoiTimNguon_updateNguoiTimNguon", parameters, out numAffected, connec);

			return numAffected;
		}

		// add a new question
		// Them ban ghi hien hanh
        public int insertNguoiTimNguon(string strHoTen, string strDiaChi, string strDienThoai, string strEmail,
                                           string strDiDong, string strFax, string strGhiChu, bool boolGioiTinh, int NguonID)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters =  {  new SqlParameter("@HoTen", SqlDbType.NVarChar, 256),
                                            new SqlParameter("@DiaChi", SqlDbType.NVarChar, 256),
                                            new SqlParameter("@DienThoai", SqlDbType.VarChar, 20),                                       
                                            new SqlParameter("@Email", SqlDbType.VarChar, 64),
                                            new SqlParameter("@DiDong", SqlDbType.VarChar, 20),
                                            new SqlParameter("@Fax", SqlDbType.VarChar, 20),
                                            new SqlParameter("@GhiChu", SqlDbType.NVarChar, 1024),
                                            new SqlParameter("@GioiTinh", SqlDbType.Bit),
                                            new SqlParameter("@NguonID", SqlDbType.Int),
											new SqlParameter("@ID", SqlDbType.Int ,4)
										 };	
			// set the values			
            parameters[0].Value = strHoTen.Trim();
            parameters[1].Value = strDiaChi.Trim();
            parameters[2].Value = strDienThoai.Trim();
            parameters[3].Value = strEmail.Trim();
            parameters[4].Value = strDiDong.Trim();
            parameters[5].Value = strFax.Trim();
            parameters[6].Value = strGhiChu.Trim();
            parameters[7].Value = boolGioiTinh;
            parameters[8].Value = NguonID;    
			parameters[9].Direction = ParameterDirection.Output;
		
			// run the procedure
            RunProcedure("sp_NguoiTimNguon_insertNguoiTimNguon", parameters, out numAffected, connec);

			return (int)parameters[9].Value;
		}
		// Xoa Role
		public int removeNguoiTimNguon(int NguoiTimNguonID)
		{
			int numAffected;
			// create the parameters
            SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int) };
			parameters[0].Value = NguoiTimNguonID;			
			// run the procedure
            try
            {
                RunProcedure("sp_NguoiTimNguon_removeNguoiTimNguon", parameters, out numAffected, connec);
            }
            catch
            {
                numAffected = 0;
            }
			return numAffected;			
		}
        // Xoa Nguon
        public int removeNguoiTimNguonbyNguon(int NguonID)
        {
            int numAffected;
            // create the parameters
            SqlParameter[] parameters = { new SqlParameter("@NguonID", SqlDbType.Int) };
            parameters[0].Value = NguonID;
            // run the procedure
            try
            {
                RunProcedure("sp_NguoiTimNguon_removeNguon", parameters, out numAffected, connec);
            }
            catch
            {
                numAffected = 0;
            }
            return numAffected;
        }
	}
}